from haystack import indexes
from course import models

class CourseIndex(indexes.SearchIndex, indexes.Indexable):
    #全文搜索的字段
    text=indexes.CharField(document=True, use_template=False)
    #索引字段 model_attr关联模型字段哪个属性
    coursename=indexes.CharField(model_attr='coursename')
    coursedesc=indexes.CharField(model_attr='coursedesc')
    price=indexes.DecimalField(model_attr='price')
    id=indexes.IntegerField(model_attr='id')
    cover=indexes.CharField(model_attr='cover')
    content=indexes.CharField(model_attr='content')
    def get_model(self):
        return models.Course
    def index_queryset(self, using=None):
        return self.get_model().objects.filter(flag=True)
    #设置text字段关联的字段
    def prepare_text(self, obj):#当前行的模型对象
        return u"%s %s %s" %(obj.coursename, obj.coursedesc, obj.content)